package com.tlw.practice.tlw.controller;

import cn.hutool.db.sql.Direction;
import com.tlw.practice.tlw.common.CommonResult;
import com.tlw.practice.tlw.entity.LeaveEntity;
import com.tlw.practice.tlw.entity.UserEntity;
import com.tlw.practice.tlw.pojo.form.RequestLeaveForm;
import com.tlw.practice.tlw.pojo.form.LeaveForm;
import com.tlw.practice.tlw.service.LeaveService;
import com.tlw.practice.tlw.service.UserService;
import com.tlw.practice.tlw.utils.DateFormatUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(tags = "请假单管理")
@RequestMapping("/leave")
public class LeaveController {

    @Autowired
    private UserService userService;
    @Autowired
    private LeaveService leaveService;

    @ApiOperation("分页查询")
    @GetMapping("/findpage/{page}/{size}/{query}")
    public CommonResult blogs(@PathVariable("page") int page, @PathVariable("size") int size,
                                  @PathVariable String query) throws ParseException {
        // 实例结果集
        Page<LeaveEntity> resultPage;
        System.err.println(query);
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "leaveTime");
        // 构造pageable
        Pageable pageable = PageRequest.of(page, size);
        if (query != null ){
            resultPage = leaveService.findRequestLeaveQuery(pageable, query);
        } else {
            resultPage = leaveService.findRequestLeaveNoQuery(pageable);
        }
        resultPage = leaveService.findRequestLeaveNoQuery(pageable);
        System.err.println(resultPage.getContent());
        return CommonResult.success(resultPage);
    }

    @ApiOperation("获取单个请假单")
    @GetMapping("/findone/{id}")
    public CommonResult<LeaveForm> findOneRequestLeave(@RequestParam("id") String id){
        LeaveEntity requestLeave = leaveService.findOneRequestLeaveById(id);
        return CommonResult.success(requestLeave);
    }

    @ApiOperation("新增请假单")
    @PostMapping("/add")
    public CommonResult<LeaveForm> addRequestLeave(@RequestBody LeaveForm requestLeave) throws ParseException {
        LeaveEntity leaveEntity = new LeaveEntity();
        BeanUtils.copyProperties(requestLeave, leaveEntity);
        // 判断请假单id是否为空
        if (requestLeave.getId() == null){
            leaveEntity.setId(UUID.randomUUID().toString());
        } else {
            LeaveEntity leave = leaveService.findOneRequestLeaveById(requestLeave.getId());
            // 如果有id，查看是否是之前保存的
            if (leave.getState() == 0){
                leaveEntity.setId(requestLeave.getId());
            } else {
                leaveEntity.setId(UUID.randomUUID().toString());
            }

        }
        // 添加创建时间
        leaveEntity.setLeaveTime(new Date());
        // 添加开始结束时间
        leaveEntity.setLeaveDateStart(requestLeave.getLeaveDateStart().getDate());
        leaveEntity.setLeaveDateEnd(requestLeave.getLeaveDateEnd().getDate());
        if (Integer.parseInt(requestLeave.getLeaveDateStart().getTime()) > 8){
            leaveEntity.setLeaveTimeStart("下午");
        } else {
            leaveEntity.setLeaveTimeStart("上午");
        }
        if (Integer.parseInt(requestLeave.getLeaveDateEnd().getTime()) > 12){
            leaveEntity.setLeaveTimeEnd("下午");
        } else {
            leaveEntity.setLeaveTimeEnd("上午");
        }
        leaveEntity.setLeaveDays(requestLeave.getLeaveDays());
        leaveEntity.setState(requestLeave.getLeaveState());
        int result = leaveService.addRequestLeave(leaveEntity);
        return CommonResult.success(result);
    }
    @ApiOperation("更新请假单")
    @PutMapping("/update")
    public CommonResult<LeaveForm> updateRequestLeave(@RequestBody LeaveForm requestLeave) throws InterruptedException {
        int result = leaveService.updateRequestLeave(requestLeave, requestLeave.getId());
        return CommonResult.success(result);
    }

    @ApiOperation("删除请假单")
    @DeleteMapping("/delete/{id}")
    public CommonResult<LeaveForm> deleteRequestLeave(@PathVariable("id") String id){
        return CommonResult.success(leaveService.deleteRequestLeave(id));
    }
}
